<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/img/logo.jpg"/>
      </div>
      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginFormRules"
          label-width="0px"
          class="login_form"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入登录账号" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              v-model="loginForm.password"
              type="password"
          ></el-input>
        </el-form-item>
        <el-form-item style="position: relative;" prop="code">
          <el-input
              placeholder="请输入验证码"
              v-model="loginForm.code"
              type="text"
              style="width: 200px;height: 30px"
          ></el-input>
          <img style="width: 150px;height: 40px;margin-left: 50px;position: absolute;" :src="imgUrl" @click="getCode"/>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click.prevent="login">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


  </div>
</template>
<script>
export default {
  data() {
    return {
      imgUrl: '',
      loginForm: {
        username: "",
        password: "",
        code: '',
        codeUuid: ''
      },
      loginFormRules: {
        username: [
          {required: true, message: "用户名必填", trigger: "blur"},
          {min: 4, max: 8, message: "用户名在4到8个字符之间", trigger: "blur"}
        ],
        password: [
          {required: true, message: "密码必填", trigger: "blur"},
          {
            min: 4,
            max: 8,
            message: "用户密码在4到8个字符之间",
            trigger: "blur"
          }
        ],
        code: [
          {required: true, message: "验证码必填", trigger: "blur"},
        ],
      }
    };
  },
  methods: {
    resetLogin() {
      this.$refs.loginForm.resetFields();
    },

    login() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) return;

        let url = "/manager/login"
        this.$axios.post(url, this.loginForm)
            .then((res) => {
              console.log(res)
              if (res.data.code == 200) {
                //把token 保存在 sessionStorage
                sessionStorage.setItem("token", res.data.token);
                sessionStorage.setItem("username", res.data.data.username);
                sessionStorage.setItem("id", res.data.data.id);
                this.$store.commit("setUsername",res.data.data.username);
                this.$store.commit("setId",res.data.data.id);
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                //跳转到主页
                this.$router.push("Home")
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
      });
    },
    getCode() {
      let url = "/manager/code"
      this.$axios.get(url).then((res) => {
        this.imgUrl = res.data.img;
        this.loginForm.codeUuid = res.data.uuid;
      })
    }
  },
  created() {
    this.getCode();
  }
};
</script>
<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 915px;
  background-image: url("../../assets/img/loginbg.png");
}

.login_box {
  width: 450px;
  height: 340px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 100px;
    width: 100px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.btns {
  display: flex;
  justify-content: center;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>